package cn.jishiyu11.xeqb.activity.personalCenterActivitys

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView

import org.json.JSONObject

import java.io.IOException
import java.util.HashMap

import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.activity.LogoActivity
import cn.jishiyu11.xeqb.activity.personalCenterActivitys.settingpage.AttentionActivity
import cn.jishiyu11.xeqb.activity.personalCenterActivitys.settingpage.HelpCenterActivity
import cn.jishiyu11.xeqb.activity.personalCenterActivitys.settingpage.SetUpPasswordActivity
import cn.jishiyu11.xeqb.base.BaseActivity
import cn.jishiyu11.xeqb.http.http.i.DataCallBack
import cn.jishiyu11.xeqb.http.http.i.httpbase.*
import cn.jishiyu11.xeqb.utils.*
import cn.jishiyu11.xeqb.utils.CameraUtils.UserCenterRealize
import cn.jishiyu11.xeqb.view.PublicPhoneDialog
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.act_set_up.*
import kotlinx.android.synthetic.main.tab_activity_fra.*
import okhttp3.Request

/**
 * Created by vvguoliang on 2017/6/28.
 *
 * 设置
 */

class SettingActivity : BaseActivity(), View.OnClickListener, DataCallBack {

    private var update_url = ""

    private var dialog: PublicPhoneDialog? = null

    private val userCenterRealize = UserCenterRealize()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_set_up)
        findViewById()
    }

    override fun onClick(v: View) {
        when (v) {
        //后退
            title_image -> finish()
        //联系我们
            set_up_link_us -> startActivity(Intent(this, AttentionActivity::class.java))
        //帮助中心
            set_up_helpcenter -> startActivity(Intent(this, HelpCenterActivity::class.java))
        //关于我们
//            set_up_about -> startActivity(Intent(this, AboutUsActivity::class.java))

        //修改密码
            set_up_password -> {
                intent = Intent(this, SetUpPasswordActivity::class.java)
                intent!!.putExtra("name", "2")
                intent!!.putExtra("type", 1)
                startActivity(intent)
            }
        //退出账号
            set_up_sign_out -> if (
                    !TextUtils.isEmpty(SharedPreferencesUtils.get(this, getString(R.string.phoneNo), "").toString() + "") &&
                    !TextUtils.isEmpty(SharedPreferencesUtils.get(this, getString(R.string.userNo), "").toString() + "") &&
                    !TextUtils.isEmpty(SharedPreferencesUtils.get(this, getString(R.string.token), "").toString() + "")

            ) {
                getPhone("提示", "您是否确认安全退出?")
            } else {
                this.showShortToast("未登录  ")
            }

        //检测版本
            set_up_sign_upde -> {
                //    getUPDATE()
            }
        }

    }

    private fun getUPDATE() {
        val map = HashMap<String, Any>()
        map.put("version", AppUtil.getInstance().getVersionName(1, this))
        OkHttpManager.postAsync(UPDATE, "update", map, this)
    }

    override fun findViewById() {
        act_set_up_appversion.text = getString(R.string.version) + " v" + PhoneAppVersion
        title_image.visibility = View.VISIBLE
        title_image.setOnClickListener(this)
        (title_view as TextView).text = this.getString(R.string.name_loan_personal_setup)
        set_up_helpcenter.setOnClickListener(this)
        set_up_link_us.setOnClickListener(this)
//        set_up_about.setOnClickListener(this)
        set_up_password.setOnClickListener(this)
        set_up_sign_out.setOnClickListener(this)
        set_up_sign_upde.setOnClickListener(this)
    }

    override fun initView() {

    }

    private fun getPhone(title: String, msg: String) {
        val builder = PublicPhoneDialog.Builder(this)
//        builder.setTitle(title)
        builder.setTiltleMsg(msg)
        builder.setContentViewCancel("取消", object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface?, which: Int) {
                dialog?.dismiss()
            }
        })
        builder.setContentViewDetermine("确定", object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface?, which: Int) {
                getUserLogoutAuthorStatus()
                logout()
            }
        })
//        builder.setContentViewCancel("取消") { dialog, which -> dialog.dismiss() }
//        builder.setContentViewDetermine("确定") { dialog, which ->  getUserLogoutAuthorStatus() }


        dialog = builder.create()
        dialog?.show()
    }

    override fun requestFailure(request: Request, name: String, e: IOException) {

    }

    @Throws(Exception::class)
    override fun requestSuccess(result: String, name: String) {
        var objects: JSONObject? = null

        if (name.equals(EXIT_LOGIN)) {
            objects = JSONObject(result)
            //安全退出
            if (result.contains("reqData")) {
                var data = objects.get("reqData").toString()
                var key = SharedPreferencesUtils.get(this, getString(R.string.token), "").toString().substring(16, 32)
                var keydCode = AESUtils.decrypt(key, data)//解密
                objects = JSONObject(keydCode)
                if (objects.get("code").equals("200")) {
                    //修改成功
//                    USER_PhoneNo = ""
//                    USER_UserNo = ""
                    this.showShortToast("安全退出成功！")
//                    newIntent<LogoActivity>()
//                    SharedPreferencesUtils.logoutSuccess(this)
//                    dialog?.dismiss()
//                    mainActivity?.finish()
//                    finish()

//                    logout()
//                    this.showShortToast(objects.getString("msg"))
                } else {
                    this.showShortToast(objects.get("msg").toString())
                }
            }
        }
    }

    private fun logout(){
        USER_PhoneNo = ""
        USER_UserNo = ""
        this.showShortToast("安全退出成功！")
        newIntent<LogoActivity>()
        SharedPreferencesUtils.logoutSuccess(this)
        dialog?.dismiss()
        mainActivity?.finish()
        finish()
    }


    private fun getPhone1(msg: String, phone: String) {
        val builder = PublicPhoneDialog.Builder(this)
        builder.setTiltleMsg(msg)
        if (phone == "2") {
            builder.setTitle("建议更新")
            builder.setContentViewCancel("取消",object :DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {

                }
            })
//            builder.setContentViewCancel("取消") { dialog, which -> dialog.dismiss() }
        } else {
            builder.setTitle("强制更新")
        }

        builder.setContentViewDetermine("确定",object :DialogInterface.OnClickListener{
            override fun onClick(dialog: DialogInterface?, which: Int) {
                userCenterRealize.getUpdata(this@SettingActivity, update_url)
                dialog?.dismiss()
            }
        })
//        builder.setContentViewDetermine("确定") { dialog, which ->
//            userCenterRealize.getUpdata(this@SettingActivity, update_url)
//            dialog.dismiss()
//        }
        builder.create().show()
    }

    private fun getUserLogoutAuthorStatus() {
        var map = HashMap<String, Any>()
        var postMap = HashMap<String, Any>()
        map.put(getString(R.string.phoneNo), USER_PhoneNo)
        getPostMap(this, EXIT_LOGIN, map, postMap, SharedPreferencesUtils.get(this, getString(R.string.token), "").toString())
        OkHttpManager.postAsync(ANDROID_HTTP, EXIT_LOGIN, postMap, this)
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

}
